home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / pcl4w13.zip / SELFTEST.C < prev    next >
Text File  |  1996-07-14  |  8KB  |  268 lines

  1. /*
  2. **                    --- selftest.c ---
  3. **
  4. **  EXAMPLE CODE: Very simple terminal emulator.
  5. **
  6. **  This example program (not the PCL4W library) is donated to
  7. **  the Public Domain by MarshallSoft Computing, Inc. It is
  8. **  provided as an example of the use of the PCL4W.
  9. **
  10. */
  11.  
  12. #define USECOMM
  13.  
  14. #include "windows.h"
  15. #include "stdio.h"
  16. #include "pcl4w.h"
  17. #include "sioerror.h"
  18. #include "ascii.h"
  19. #include "selftest.h"
  20. #include "message.h"
  21. #include "line.h"
  22. #include "paint.h"
  23. #include "runtest.h"
  24. #include "about.h"
  25.  
  26. /* public globals */
  27.  
  28. HWND hMainWnd;            /* main window handle */
  29. int OnLineFlag = FALSE;   /* TRUE: online */
  30. int FatalFlag = FALSE;    /* TRUE: fatal error */
  31. char Temp[1024];          /* temporary 1K buffer */
  32.  
  33. /* private globals */
  34.  
  35. static HMENU hMenu;
  36. static HANDLE hInstance;
  37. static int ThePort = COM1;
  38. static int WinWidth = 8 * NCOLS;
  39. static int WinHeight = 12 * NROWS + 48;
  40.  
  41. /* miscellaneous functions */
  42.  
  43. void ErrorCheck(int);
  44.  
  45. int PASCAL WinMain(HANDLE hInst,HANDLE hPrevInstance,
  46.                    LPSTR lpCmdLine,int nCmdShow)
  47. {WNDCLASS  wc;
  48.  MSG msg;
  49.  BOOL Result;
  50.  if(!hPrevInstance)
  51.    {/* register main window class */
  52.     wc.style = CS_HREDRAW | CS_VREDRAW;
  53.     wc.lpfnWndProc = MainWndProc;
  54.     wc.cbClsExtra = 0;
  55.     wc.cbWndExtra = 0;
  56.     wc.hInstance = hInst;
  57.     wc.hIcon = LoadIcon(hInst, "SelftestIcon");
  58.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  59.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  60.     wc.lpszMenuName =  "SelftestMenu";
  61.     wc.lpszClassName = "SelftestWClass";
  62.     Result = RegisterClass(&wc);
  63.     if(!Result) return FALSE;
  64.    }
  65.  
  66.  /* create main window */
  67.  hInstance = hInst;
  68.  hMainWnd = CreateWindow(
  69.         "SelftestWClass", "Selftest",     WS_OVERLAPPEDWINDOW,
  70.         CW_USEDEFAULT,    CW_USEDEFAULT,
  71.         WinWidth,         WinHeight,
  72.         NULL,             NULL,
  73.         hInstance,        NULL);
  74.  ShowWindow(hMainWnd, nCmdShow);
  75.  UpdateWindow(hMainWnd);
  76.  hMenu = GetMenu(hMainWnd);
  77.  
  78.  /* window control loop */
  79.  
  80.  while(GetMessage(&msg,NULL,NULL,NULL))
  81.    {
  82.     TranslateMessage(&msg);
  83.     DispatchMessage(&msg);
  84.    }
  85.  return (msg.wParam);
  86. } /* end WinMain */
  87.  
  88. long FAR PASCAL MainWndProc(HWND hWindow,UINT message,WPARAM wParam,LPARAM lParam)
  89. {
  90.  UINT idTimer;
  91.  HDC hDC;
  92.  PAINTSTRUCT ps;
  93.  int i;
  94.  int TheChar;
  95.  int Count;
  96.  static FARPROC lpProcAbout;
  97.  
  98.  
  99.  hMainWnd = hWindow;
  100.  switch (message)
  101.    {
  102.     case WM_COMMAND:
  103.        switch(wParam)
  104.            {case MSG_ABOUT:
  105.               DialogBox(hInstance,"AboutBox",hMainWnd,lpProcAbout);
  106.               break;
  107.  
  108.             case MSG_TEST:
  109.               if(OnLineFlag) RunTest(ThePort);
  110.               else DisplayLine("Must be online");
  111.               break;
  112.  
  113.             case MSG_INSTRUCT:
  114.               DisplayLine("\nConnect a loopback adapter to your port cable. Refer to LOOPBACK.DOC");
  115.               DisplayLine("for instructions on how to make a loopback adapter. Very easy!");
  116.               DisplayLine("For maximum performance, Windows port virtualization must be disabled.");
  117.               DisplayLine("Refer to the users manual PCL4WUSR.DOC\n");
  118.               break;
  119.  
  120.             case MSG_DEBUG:
  121.               break;
  122.  
  123.             case MSG_ONLINE:
  124.               if(OnLineFlag) break;
  125.               else
  126.                 {/* try to go on-line */
  127.                  if(GoOnLine(ThePort,Baud19200,Size1024,Size1024))
  128.                    {/* we're online */
  129.                     OnLineFlag = TRUE;
  130.                     SetWindowText(hMainWnd,"SELFTEST: Online @ 19200");
  131.                     CheckMenuItem(hMenu,MSG_OFFLINE,MF_BYCOMMAND | MF_UNCHECKED);
  132.                     CheckMenuItem(hMenu,MSG_ONLINE,MF_BYCOMMAND | MF_CHECKED);
  133.                     for(i=COM1;i<=COM4;i++)
  134.                       EnableMenuItem(hMenu,MSG_COM1+i,MF_BYCOMMAND|MF_GRAYED);
  135.                    }
  136.                 }
  137.               break;
  138.  
  139.             case MSG_OFFLINE:
  140.               if(!OnLineFlag) break;
  141.               GoOffLine(ThePort);
  142.               OnLineFlag = FALSE;
  143.               SetWindowText(hMainWnd,"SELFTEST: Offline");
  144.               CheckMenuItem(hMenu,MSG_ONLINE,MF_BYCOMMAND | MF_UNCHECKED);
  145.               CheckMenuItem(hMenu,MSG_OFFLINE,MF_BYCOMMAND | MF_CHECKED);
  146.               for(i=COM1;i<=COM4;i++)
  147.                 EnableMenuItem(hMenu,MSG_COM1+i,MF_BYCOMMAND|MF_ENABLED);
  148.               break;
  149.  
  150.             case MSG_EXIT:
  151.               DestroyWindow(hMainWnd);
  152.               break;
  153.  
  154.             case MSG_COM1:
  155.               CheckMenuItem(hMenu,MSG_COM1+ThePort,MF_BYCOMMAND | MF_UNCHECKED);
  156.               ThePort = COM1;
  157.               CheckMenuItem(hMenu,MSG_COM1,MF_BYCOMMAND | MF_CHECKED);
  158.               break;
  159.  
  160.             case MSG_COM2:
  161.               CheckMenuItem(hMenu,MSG_COM1+ThePort,MF_BYCOMMAND | MF_UNCHECKED);
  162.               ThePort = COM2;
  163.               CheckMenuItem(hMenu,MSG_COM2,MF_BYCOMMAND | MF_CHECKED);
  164.               break;
  165.  
  166.             case MSG_COM3:
  167.               CheckMenuItem(hMenu,MSG_COM1+ThePort,MF_BYCOMMAND | MF_UNCHECKED);
  168.               ThePort = COM3;
  169.               CheckMenuItem(hMenu,MSG_COM3,MF_BYCOMMAND | MF_CHECKED);
  170.               break;
  171.  
  172.             case MSG_COM4:
  173.               CheckMenuItem(hMenu,MSG_COM1+ThePort,MF_BYCOMMAND | MF_UNCHECKED);
  174.               ThePort = COM4;
  175.               CheckMenuItem(hMenu,MSG_COM4,MF_BYCOMMAND | MF_CHECKED);
  176.               break;
  177.  
  178.             default:
  179.               return (DefWindowProc(hMainWnd, message, wParam, lParam));
  180.            }
  181.          break;
  182.  
  183.     case WM_CREATE:
  184.  
  185.       /* check "OFFLINE" menu item */
  186.       hMenu = GetMenu(hMainWnd);
  187.       CheckMenuItem(hMenu,MSG_OFFLINE,MF_BYCOMMAND | MF_CHECKED);
  188.       /* check default COM port */
  189.       CheckMenuItem(hMenu,MSG_COM1+ThePort,MF_BYCOMMAND | MF_CHECKED);
  190.       /* create AboutDlgProc() thunk */
  191.       lpProcAbout = MakeProcInstance(AboutDlgProc, hInstance);
  192.       /* initialize paint module */
  193.       InitPaint();
  194.       /* start timer */
  195.       idTimer = SetTimer(hMainWnd,1,125,NULL);
  196.       if(idTimer==0)
  197.          {MessageBox(hMainWnd,"No timers remaining !","ERROR",MB_ICONEXCLAMATION | MB_OK);
  198.           FatalFlag = TRUE;
  199.          }
  200.       break;
  201.  
  202.     case WM_CHAR:
  203.       SioPutc(ThePort, (char)wParam );
  204.       break;
  205.  
  206.     case WM_TIMER:
  207.       /* fatal error ? */
  208.       if(FatalFlag) break;
  209.       if(!OnLineFlag) break;
  210.       /* fetch line of up to 1024 chars */
  211.       Count = 0;
  212.       for(i=0;i<1024;i++)
  213.         {TheChar = SioGetc(ThePort,0);
  214.          /* character available ? */
  215.          if(TheChar==-1) break;
  216.          Temp[Count++] = TheChar;
  217.          if((char)TheChar==(char)LF) break;
  218.         } /* end while */
  219.       if(Count>0) WriteTheString(Temp,Count);
  220.       break;
  221.  
  222.     case WM_SETFOCUS:
  223.       /* create client area caret */
  224.       CreateCaret(hMainWnd,NULL,3,10);
  225.       SetCaretPos(GetXposition(),GetYposition());
  226.       ShowCaret(hMainWnd);
  227.       ShowCaret(hMainWnd);
  228.       break;
  229.  
  230.     case WM_KILLFOCUS:
  231.       DestroyCaret();
  232.       break;
  233.  
  234.     case WM_PAINT:
  235.       HideCaret(hMainWnd);
  236.       hDC = BeginPaint(hMainWnd, &ps);
  237.       SetMapMode(hDC,MM_ANISOTROPIC);
  238.       SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  239.       PaintMain(hDC,&ps);
  240.       EndPaint(hMainWnd,&ps);
  241.       SetCaretPos(GetXposition(),GetYposition());
  242.       ShowCaret(hMainWnd);
  243.       break;
  244.  
  245.     case WM_DESTROY:
  246.       GoOffLine(ThePort);
  247.       SetWindowText(hMainWnd,"SELFTEST: Offline");
  248.       CheckMenuItem(hMenu,MSG_OFFLINE,MF_BYCOMMAND | MF_CHECKED);
  249.       CheckMenuItem(hMenu,MSG_ONLINE,MF_BYCOMMAND | MF_UNCHECKED);
  250.       if(idTimer) KillTimer(hMainWnd,idTimer);
  251.       PostQuitMessage(0);
  252.       break;
  253.  
  254.     default:
  255.       return (DefWindowProc(hMainWnd, message, wParam, lParam));
  256.    }
  257.  return (NULL);
  258. } /* end MainWndProc */
  259.  
  260. void ErrorCheck(int Code)
  261. {/* trap PCL error codes */
  262.  if(Code<0)
  263.      {SioError(Code,"Sio Error");
  264.       SioDone(ThePort);
  265.       FatalFlag = TRUE;
  266.      }
  267. } /* end ErrorCheck */
  268.